<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
</head>

<body>
<?PHP
echo phpinfo();
exit;
include("common/connect.php");
include("common/common.php");
include("taobaoapi/TopSdk.php");

echo "start<br>";
$dNow = date('Y-m-j');
echo "今天是： " .$dNow;

// 上一次取的时间和上一次取的TradeId，这2个是用来作增量的依据
$lastDate = mktime(0, 0, 0, 1, 1, 2011);
$lastTradeId = "";

// 取上一个时间
$result = mysql_query( "select * from report_items2 order by pay_time desc limit 1" );
if ($result)
{
	$row = mysql_fetch_assoc( $result );
	$lastDate = strtotime($row["pay_time"], $lastDate);
	$lastTradeId = $row["trade_id"];
	echo "<br>上一条记录时间是：".date('Y-m-j G:i:s', $lastDate) . " 对应TradeId是：" . $lastTradeId;
}

$nNeedDay = intval((time() - $lastDate) / (3600 * 24));
//echo "<br>".strtotime(time());
//echo "<br>".time()."  ".$lastDate ."  ".(time() - $lastDate);
echo "<br>需要取历史 ".$nNeedDay." 天的记录.";


// 开始取
$c = new TopClient;
$c->appkey = "12175045";
$c->secretKey = "fecb88ed680dc756af6fef24cc62b27f";
$req = new TaobaokeReportGetRequest;
$req->setFields("trade_id,pay_time,pay_price,num_iid,outer_code,commission_rate,commission,seller_nick,app_key,item_num,item_title,category_id,category_name,shop_title");
$req->setPageSize(40);

$nAllCount = 0;
for ($i = 0; $i < $nNeedDay; ++$i)
{
	echo "<br>";
	$dCurDate = $lastDate + $i * 3600 * 24;
	echo date('Y-m-j', $dCurDate);
	GetOneDateData($c, $req, $resp, date('Ymj', $dCurDate), $lastTradeId);
}

echo "<br>开始取今天的记录: ".date('Y-m-j G:i:s');
GetOneDateData($c, $req, $resp, date('Ymj'), $lastTradeId);

function GetOneDateData($c, $req, $resp, $strCurDay, $lastTradeId)
{
	echo "<br>GetOneDateData, day=".$strCurDay;
	$req->setDate($strCurDay);
	for ($page = 1; $page < 99; ++$page)
	{
		echo "<br>page:" .$page. "<br>";
		$req->setPageNo($page);
		$resp = $c->execute($req);
		if ($resp->getName() == "error_response")
		{
			echo "error_response";
			break;
		}
	
		$breakFlag = 0;
		foreach ($resp->taobaoke_report->taobaoke_report_members[0] as $Item)
		{
			if ($Item->trade_id == $lastTradeId)
			{
				echo "<br>取到上一条TradeId，增量已经取完， TradeId： ".$lastTradeId."时间是：".$Item->pay_time;
				$breakFlag = 1;
				break;
			}
			
			//
			echo $nAllCount;
			echo "   |   ";
			echo $Item->pay_time;
			echo "   |   ";
			echo $Item->seller_nick;
			echo "   |   ";
			echo $Item->outer_code;
			echo "<br>";
			$nAllCount += 1;
			
			$sql = "insert into report_items2 (			app_key,trade_id,pay_time,pay_price,num_iid,outer_code,commission_rate,commission,seller_nick,item_num,item_title,category_id,category_name,shop_title) 
				values (
					'".$Item->app_key."',
					'".$Item->trade_id."',
					'".$Item->pay_time."',
					'".$Item->pay_price."',
					'".$Item->num_iid."',
					'".$Item->outer_code."',
					'".$Item->commission."',
					'".$Item->commission_rate."',
					'".$Item->seller_nick."',
					'".$Item->item_num."',
					'".$Item->item_title."',
					'".$Item->category_id."',
					'".$Item->category_name."',
					'".$Item->shop_title."')";
			mysql_query( $sql );
			if (mysql_affected_rows() == 0)
			{
				echo "<script>alert('处理过程中发生失败，请告诉程序员，出错代码：1');";
				exit;
			}
		}
		
		if ($breakFlag == 1)
			break;
		
		if ($nAllCount % 40 != 0)	// 淘宝api有点怪，不知道判断已经取完了
			break;
	}
}

echo "<br>";
echo $nAllCount;
echo "<br>end";
//print_r($resp);
?>


</body>
</html>